package me.zixing.java.chapter08.task01;

/**
 * Created by bingh on 2017/11/10.
 */
public class Student implements PropertiesFillable{
    private static final int stuNoLength = 9;

    private static final int stuNameLength = 4;
    private static final int stuAgeLength = 3;
    public static final int stuAllLength = stuNoLength + stuNameLength + stuAgeLength;
    private String studentNo;


    private String studentName;
    private int studentAge;
    public Student() {
    }

    public Student(String studentNo, String studentName, int studentAge) {
        this.studentNo = studentNo;
        this.studentName = studentName;
        this.studentAge = studentAge;
    }

    @Override
    public void fillObjectPropertiesWith(String propertiesStr) {
        if(propertiesStr == null){
            return;
        }
        char[] chars = new char[propertiesStr.length()];
        propertiesStr.getChars(0,propertiesStr.length(),chars,0);
        if(chars[0] == '\ufeff'){//解决UTF-8 BOM问题
            propertiesStr = propertiesStr.substring(1);
        }
        try{
            this.setStudentNo(propertiesStr.substring(0,stuNoLength).trim());
            this.setStudentName(propertiesStr.substring(stuNoLength,stuNoLength+stuNameLength-1).trim());
            this.setStudentAge(
                    Integer.parseInt(propertiesStr.substring(stuNoLength+stuNameLength).trim()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getStudentNo() {
        return studentNo;
    }

    public void setStudentNo(String studentNo) throws StudentSetException{
        if(studentNo.length() != 9){
            throw new StudentSetException("学号必须9位");
        }
        this.studentNo = studentNo;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) throws StudentSetException{
        if(studentName.length() > 4){
            throw new StudentSetException("姓名不能大于4位");
        }
        this.studentName = studentName;
    }

    public int getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(int studentAge) throws StudentSetException{
        if(studentAge < 0 ){
            throw new StudentSetException("年龄不能为负数");
        }
        this.studentAge = studentAge;
    }

    @Override
    public String toString() {
        String no = studentNo;
        String name = formatName(studentName);
        String age = formatAge(studentAge);
        return no+name+age+"\n";
    }

    public String formatName(String name){
        while(stuNameLength - name.length() > 0){
            name += " ";
        }
        return name;
    }

    public String formatAge(int age){
        String ageString = String.valueOf(age);
        while(stuAgeLength - ageString.length() > 0){
            ageString += " ";
        }
        return ageString;
    }

   /* public static void main(String[] args) throws Exception{
        Student student = new Student();
        student.setStudentAge(22);
        student.setStudentName("李想");
        student.setStudentNo("000000001");

        System.out.println(student);
    }
*/
}
